package com.tim.app.util;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.application.library.util.PhoneUtil;
import com.application.library.widget.SimpleAnimationListener;


/**
 * create at 2016-07-23
 */
public class AnimUtils {
    private static AnimUtils mAnimUtils;

    private AnimUtils() {
    }

    public static AnimUtils ins() {
        if (mAnimUtils == null) {
            mAnimUtils = new AnimUtils();
        }
        return mAnimUtils;
    }

    public void addSlideInAnim(final View view) {
        AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);// 必须添加不然出现误差
        alpha.setDuration(800);
        view.startAnimation(alpha);
        alpha.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }
        });
    }

    public void addSlideOutAnim(final View view) {
        AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);// 必须添加不然出现误差
        alpha.setDuration(800);
        view.startAnimation(alpha);
        alpha.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                view.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }
        });
    }

    public void videoMaskShowAnim(Context context, View topView, View bottomView, View shadowView, SimpleAnimationListener animationListener) {
        int duration = 200;
        ObjectAnimator alpha_shadow_top = ObjectAnimator.ofFloat(shadowView, "alpha", 0f, 1f);
        ObjectAnimator top_trans = ObjectAnimator.ofFloat(topView, "translationY", PhoneUtil.dipToPixel(-45, context), 0f);
        ObjectAnimator bottom_trans = ObjectAnimator.ofFloat(bottomView, "translationY", PhoneUtil.dipToPixel(50, context), 0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(alpha_shadow_top, top_trans, bottom_trans);
        animatorSet.setDuration(duration);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.addListener(animationListener);
        animatorSet.start();
    }

    public void videoMaskHideAnim(Context context, View topView, View bottomView, View shadowView, SimpleAnimationListener animationListener) {
        int duration = 200;
        ObjectAnimator alpha_shadow_top = ObjectAnimator.ofFloat(shadowView, "alpha", 1f, 0f);
        ObjectAnimator top_trans = ObjectAnimator.ofFloat(topView, "translationY", 0f, PhoneUtil.dipToPixel(-45, context));
        ObjectAnimator bottom_trans = ObjectAnimator.ofFloat(bottomView, "translationY", 0f, PhoneUtil.dipToPixel(50, context));
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(alpha_shadow_top, top_trans, bottom_trans);
        animatorSet.setDuration(duration);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.addListener(animationListener);
        animatorSet.start();
    }

    public void trans(ImageView view, boolean isReBack) {
        if (view == null)
            return;
        int start = 0;
        int end = 180;
        if (isReBack) {
            start = 180;
            end = 360;
        }
        ObjectAnimator trans = ObjectAnimator.ofFloat(view, "rotation", start, end);
        trans.setDuration(500);
        trans.start();
    }
}
